import Cookies from 'js-cookie'
import config from '@/config'
import User from '@/api/user.js'
import message from 'ant-design-vue/lib/message/index.js'
import { AUTH_FAILED } from '@/constants/message.js'

export default {
  namespaced: true,
  state: () => {
    return {
      token: null,
      userInfo: {},
      serverTime: undefined
    }
  },
  getters: {
    // 销售人员
    isSales: state => {
      return Array.isArray(state.userInfo.roleInfo) && state.userInfo.roleInfo.includes(11)
    },
    // 管理人员
    isManager: state => {
      return state.userInfo.isManager
    },
    // 集团销售总监
    isPrimaryCSO: state => {
      return Array.isArray(state.userInfo.roleInfo) && state.userInfo.roleInfo.includes(27)
    },
    // 分公司销售总监
    isBranchCSO: state => {
      return Array.isArray(state.userInfo.roleInfo) && state.userInfo.roleInfo.includes(26)
    },
    // 集团运营总监
    isPrimaryCOO: state => {
      return Array.isArray(state.userInfo.roleInfo) && state.userInfo.roleInfo.includes(16)
    },
    // BUM
    isBUM: state => {
      return state.userInfo.isBUM
    },
    // 销售支持
    isSalesSupport: state => {
      return Array.isArray(state.userInfo.roleInfo) && state.userInfo.roleInfo.includes(34)
    },
    // 业务人员
    isBusinessPersonnel: state => {
      return Array.isArray(state.userInfo.roleInfo) && state.userInfo.roleInfo.includes(42)
    },
    // HRBP
    isHRBP: state => {
      return state.userInfo.isManager
    },
    wiitrans: state => {
      return state.userInfo.fromGrade === 20
    },
    eci: state => {
      return state.userInfo.fromGrade === 10
    }
  },
  mutations: {
    setToken(state, token) {
      state.token = token
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    },
    setServerTime(state, time) {
      state.serverTime = time
    }
  },
  actions: {
    getServerTime({ commit }) {
      return new Promise((resolve, reject) => {
        User.getServerTime()
          .then(({ data }) => {
            commit('setServerTime', data)
            resolve()
          })
          .catch(() => {
            reject(new Error())
          })
      })
    },
    login({ commit }) {
      return new Promise((resolve, reject) => {
        const token = Cookies.get(config.COOKIE_KEY) || config.TEMP_TOKEN
        commit('setToken', token)
        if (token) {
          User.getUserInfo()
            .then(res => {
              commit('setUserInfo', res.data)
              resolve(res)
            })
            .catch(() => {
              commit('setToken', null)
              Cookies.remove(config.COOKIE_KEY)
              message.error(AUTH_FAILED, 0)
              reject(new Error(AUTH_FAILED))
            })
        } else {
          reject(new Error('Unable to get cookie!'))
        }
      })
    },
    logout({ commit }) {
      return new Promise(resolve => {
        commit('setToken', null)
        Cookies.remove(config.COOKIE_KEY, {
          path: '/',
          domain: '.eciol.com'
        })
        resolve()
      })
    }
  }
}
